<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use think\Cache;
use think\Db;
use think\Request;

class Department extends Base
{
    public function department(){
        $head = $this->request = Request::instance()->header();
        if(substr($head['token'], -1)==4){
            $this->apiReturn(101,'敢越过权限，你胆子蛮大嘛');
        }
        if(Request::instance()->method()== 'GET'){
            $page = input('current','1','intval');
            $page_size = input('pageSize',10,'intval');
            $like = I("id|department_name");
            $data = Db::name('department')->field('id,department_name')->field(['id'=>'key'])->where('status',1)
                ->where('id', 'like', '%'.$like['id'].'%')
                ->where('department_name', 'like', '%'.$like['department_name'].'%')
                ->page($page,$page_size)->select();
            $total = bs::total('department');
            $this->apiReturn(200,'',$data,$total);
        }
        if(Request::instance()->method()== 'POST'){
            $value['department_name'] = I('department_name');
            if(!$value['department_name']){
                $this->apiReturn(303,'lack '.$value['department_name']);
            }
            $value['create_time'] = time();
            $value['update_time'] = time();
            $id = bs::add('department',$value);
            $this->apiReturn(200,'',['id'=>$id]);
        }
        if(Request::instance()->method()== 'PUT'){
            $value = I('id|department_name');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            $value['update_time'] = time();
            $id = bs::edit('department',$value['id'],$value);
            $this->apiReturn(200,'',['id'=>$id]);
        }
        if(Request::instance()->method()== 'DELETE'){
            $id = input('id');
            if(!$id){
                $this->apiReturn(303,'lack id');
            }
            $return_key = bs::del('department',$id);
            $this->apiReturn(200,'',$return_key);
        }
    }

}